import h5py

# 打开两个源h5文件
file1 = 'outputs/baseline_embedding/embeddings_sup.h5'
file2 = 'outputs/baseline_embedding/embeddings.h5'
output_file = 'outputs/baseline_embedding/all_embeddings.h5'

with h5py.File(file1, 'r') as data1, h5py.File(file2, 'r') as data2, h5py.File(output_file, 'w') as out:
    # 先写入第一个文件的所有数据
    for key in data1.keys():
        out.create_dataset(key, data=data1[key][:])
    # 再写入第二个文件的数据，若key已存在则跳过
    for key in data2.keys():
        if key not in out:
            out.create_dataset(key, data=data2[key][:])
        else:
            print(f"Key {key} 已存在，跳过")

print(f"合并完成，输出文件: {output_file}")







